package com.msk.web.controller.wlhy.goodsSignatory;

import com.google.common.collect.Maps;
import com.msk.base.domain.goods.Goods;
import com.msk.base.service.PriceCompareService;
import com.msk.base.service.goods.GoodsService;
import com.msk.common.core.controller.BaseController;
import com.msk.common.core.domain.R;
import com.msk.common.core.domain.model.LoginUser;
import com.msk.common.utils.StringUtils;
import com.msk.framework.web.utils.TokenUtils;
import com.msk.sys.domain.Dict;
import com.msk.sys.utils.DictMskUtils;
import com.msk.wlhy.service.HtLogisticsOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author llh
 * @date 2023/6/15
 * @description 货主签约列表接口
 */
@Api(tags = "货主签约列表接口")
@RestController
@RequestMapping("/api/goodsSignatory")
public class goodsSignatoryController extends BaseController {
    
    @Autowired
    private GoodsService goodsService;
    
    @Autowired
    private HtLogisticsOrderService htLogisticsOrderService;
    
    @Autowired
    private PriceCompareService priceCompareService;
    
    /**
     * @author llh
     * @date 2023/6/15
     * @return com.msk.common.core.domain.R<?>
     * @description 签约列表接口      
     */
    @ApiOperation("签约列表接口")
    @PostMapping("/signedList")
    public R<?> signedList(@RequestBody Goods goods){
        LoginUser loginUser = TokenUtils.getMpLoginUser();
        if(loginUser != null && loginUser.getLoginObject() != null && "2".equalsIgnoreCase(loginUser.getLoginObject())){
            //平台给货主发货货主可以看到
            goods.setUserIdentity("2");
            goods.setCreateId(loginUser.getLoginId());
            goods.setPublishWay("3");
        }
        goods.getParams().put("orderByStr", "a.type,a.create_date desc");
        startPageJson(goods.getParams(),"");
        List<Map<String,Object>> goodsList = goodsService.findSignedList(goods);
        List<Dict> priceWayDictList = DictMskUtils.getDictListNew("price_way");
        List<Dict> publishWayDictList = DictMskUtils.getDictListNew("publish_way");
        for(Map<String,Object> map : goodsList){
            String priceWay = DictMskUtils.getDictLabelNew(String.valueOf(map.get("priceWay")), priceWayDictList, "");
            map.put("priceWayAlias",priceWay);
            String publishWay = DictMskUtils.getDictLabelNew(String.valueOf(map.get("publishWay")), publishWayDictList, "");
            map.put("publishWayAlias",publishWay);
        }
        return R.appOk(getDataTable(goodsList));
    }
    
    /**
     * @author llh
     * @date 2023/6/16
     * @return com.msk.common.core.domain.R<?>
     * @description 签约货源详情接口      
     */
    @ApiOperation("签约货源详情接口")
    @PostMapping("/getSignedInfo")
    public R<?> getSignedInfo(@RequestBody Goods goods){
        String id = goods.getId();
        if(StringUtils.isBlank(id)){
            return R.appFail(400101,"请求参数货源ID#id为空");
        }
        LoginUser loginUser = TokenUtils.getMpLoginUser();
        if(loginUser != null && loginUser.getLoginObject() != null && "2".equalsIgnoreCase(loginUser.getLoginObject())){
            //货主仅可看到自己发布的货源
            goods.setUserIdentity("2");
            goods.setCreateId(loginUser.getLoginId());
            goods.setPublishWay("3");
        }
        return R.appOk(goodsService.getSignedInfo(goods));
    }
    
    /**
     * @author llh
     * @date 2023/6/16
     * @return com.msk.common.core.domain.R<?>
     * @description 货主确认签约接口      
     */
    @ApiOperation("货主确认签约接口")
    @PostMapping("/confirmSigned")
    public R<?> confirmSigned(@RequestBody Map<String,String> params){
        Map<String,Object> result = Maps.newHashMap();
        String priceId = params.get("priceId");
        String goodsId = params.get("goodsId");
        try {
            if(StringUtils.isNotEmpty(priceId)){
                //下单管理 防止重复下单
                if(0L!=htLogisticsOrderService.getCountByGoodId(goodsId)){
                    result.put("flag", false);
                    result.put("message", "已下单成功,请勿重复操作");
                    return R.appOk(result);
                }else{
                    priceCompareService.downsOrder(goodsId,priceId);
                }
            }
            result.put("flag", true);
            result.put("message", "签约完成");
        } catch (Exception e) {
            result.put("flag", false);
            result.put("message", e.toString());
        }
        return R.appOk(result);
    }
}
